<template>
  <div>
    <!-- 添加或修改资产清单表对话框 -->
    <el-dialog ref="editDialog" v-model="openFlag" title="申请" width="50%" append-to-body>
      <el-form ref="form" :model="formData" :rules="rules" label-width="80">
        <el-row>
          <el-col :span="12">
            <el-form-item label="费用" prop="fee">
              <el-input v-model="formData.fee"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理类型" prop="handleType">
              <el-select v-model="formData.handleType">
                <el-option v-for="t in handleTypeList" :label="t.label" :value="t.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <choose-table ref="chooseTable">
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="分类" prop="name"></el-table-column>
        <el-table-column label="品牌" prop="brand"></el-table-column>
        <el-table-column label="型号" prop="model"></el-table-column>
      </choose-table>

      <div slot="footer" class="dialog-footer">
        <el-row type="flex" justify="end">
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="sub">提交</el-button>
          <el-button @click="close">取 消</el-button>
        </el-row>

      </div>
    </el-dialog>

  </div>
</template>

<script setup>

import ChooseTable from "@/components/Asset/ChooseTable.vue";
import api from "@/api/asset/apply.js";
import { handleTypeList } from "./data.js";

const emit = defineEmits(['sub']);

const props = defineProps({
  applyType: {
    type: String,
    required: true,
  }
})

const chooseTable = ref();

// 是否编辑
const isEdit = ref(false);

const applyDataInit = {
  id: null,
  applyType: props.applyType,
}

const applyData = reactive(applyDataInit);

const formDataInit = {
  fee: null,
  handleType: null,
  remark: null
}

// 表单规则
const rules = {
  handleType: {
    required: true,
    message: '不能为空',
    trigger: 'blur',
  }
}

const openFlag = ref(false);
const formData = reactive(formDataInit)

function reset() {
  Object.assign(formData, formDataInit);
}

function open(row) {
  openFlag.value = true;

  reset();
  if (row) {
    api.detail(row).then(response=> {
      Object.assign(applyData, response);
      const resFormData = response.formData;
      if (resFormData) {
        Object.assign(formData, JSON.parse(resFormData));
      }
      chooseTable.value.setList(response.relItems);
    })
    isEdit.value = true;
  } else {
    isEdit.value = false;
  }
}

function close() {
  openFlag.value = false;
}

function getParams() {
  const table = chooseTable.value;
  const apply = { ...applyData };
  apply.applyType = props.applyType;
  apply.formData = JSON.stringify(formData);
  return {
    edit: isEdit.value,
    apply: apply,
    items: table.getList(),
    itemsDel: table.getDeletedList()
  }
}

/** 保存 */
function save() {

  api.save(getParams()).then(response => {
    emit("sub");
    close();
  });
}

function sub() {
  api.saveAndSub(getParams()).then(response => {
    emit("sub");
    close();
  });
}

defineExpose({
  open
})


</script>

